package com.bamanker.cloudsys.controller;


import com.bamanker.cloudapi.domain.MyPage;
import com.bamanker.cloudsys.domain.toEmail;
import com.bamanker.cloudsys.mapper.ToEmailMapper;
import com.bamanker.cloudsys.service.ToEmailService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author bamanker
 */
@Slf4j
@RestController()
@RequiredArgsConstructor
@RequestMapping("/manager")
@PreAuthorize("hasAuthority('system')")
public class ToEmailController {


    private final ToEmailService toEmailService;

    private final ToEmailMapper mapper;

    /*restful 部分*/

    @GetMapping("/toEmails")
//    @SaCheckPermission(value = "user.get")
    public MyPage<toEmail> list(@RequestParam(value = "start", defaultValue = "1") int start,
                                @RequestParam(value = "size", defaultValue = "5") int size) {
//        Page<toEmail> pageInfo = new Page<>(start, size);
//        Page<toEmail> page = toEmailService.page(pageInfo);
//        MyPage<toEmail> myPage = new MyPage<>();
//        myPage.setList(page.getRecords());
//        myPage.setPages(page.getPages());
//        myPage.setPageNum(page.getCurrent());
//        myPage.setSize(page.getSize());
//        myPage.setHasPreviousPage(page.hasPrevious());
//        myPage.setHasNextPage(page.hasNext());
//        myPage.setPrePage(page.getCurrent() - 1);
//        myPage.setNextPage(page.getCurrent() + 1);
//        long[] navigatepageNums = new long[(int) page.getPages()];
//        for (int i = 0, l = 1; i < page.getPages(); i++) {
//            navigatepageNums[i] = l++;
//        }
//        myPage.setNavigatepageNums(navigatepageNums);
        return toEmailService.selectPage(start, size);
    }

    @GetMapping("/toEmails/{id}")
//    @SaCheckPermission(value = "user.get")
    public toEmail get(@PathVariable("id") int id) {
        toEmail t = toEmailService.getById(id);
        log.info("{},{}", id, t);
        return t;
    }

    @PostMapping("/toEmails")
//    @SaCheckPermission(value = "user.add")
    public String add(@RequestBody toEmail t) {
        mapper.insert(t);
        return "success";
    }

    @DeleteMapping("/toEmails/{id}")
//    @SaCheckPermission(value = "user.delete")
    public String delete(@PathVariable("id") Long id) {
        boolean b = toEmailService.removeById(id);
        return String.valueOf(b);
    }

    @PutMapping("/toEmails")
//    @SaCheckPermission(value = "user.update")
    public String update(@RequestBody toEmail t) {
//        toEmail toEmail = new toEmail();
//        toEmail.setId((long) id);
        boolean b = toEmailService.updateById(t);
        return String.valueOf(b);
    }


}
